<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$_SESSION['uid']=$_SESSION['uid']?:0;
if($_RQ['op']=='main'){
	//返回链接
	$return['forward_url']=base64_encode('jy_kuaidisys/ji_log//fromcreate=1');
	$isfollow=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');	
	if(!$isfollow && $_RQ['fromcreate'] && !$shopset['noshowqrcode']){
		$return['showwechatcode']=1;
	}
	$return['type']='log';
	$return['params']=array('status'=>100);
	$return['list']=array();
	set_html('title','订单列表');
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	if(isset($_RQ['status']) && $_RQ['status'] !=100){
		$params['status']=intval($_RQ['status']);
		$return['status']=$return['params']['status']=$_RQ['status'];
	}else{
		$return['status']=100;
	}
	if(!empty($_RQ['ordersn'])){
		$params['ordersn OR']='`ordersn` = \''.trim($_RQ['ordersn']).'\' or `expresssn` =\''.trim($_RQ['ordersn']).' \' ';
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_kuaidisys_jilog',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	foreach($list as &$item){
		$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		$item['statustitle']=$this->jilog_status[$item['status']];
		$item['address']=json_decode($item['address'],true);
		$item['toaddress']=json_decode($item['toaddress'],true);
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='pay'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_kuaidisys_jilog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi($order['pay_id']);
}
if($_RQ['op']=='cancel'){
	$post=get_postdata();
	$id=intval($post['id']);
    $status=intval($post['status']);
    if($status){
        $order=$this->pdo_get('jilog',array('id'=>$id));
        $params=array(
            'type'=>0,
            'orderid'=>$id,
            'price'=>$order['money'],
            'content'=>'客户申请',
        );
        $this->model('refund')->refund_create($params);
    }else{
        $this->model('model')->jilog_cancel($id);
    }
	exi('取消成功！');	
}
//支付退
if($_RQ['op']=='tuipay'){
	$id=intval($_RQ['id']);
	$order=pdo_get('jy_kuaidisys_jilog',array('id'=>$id));
	$bufee=0-$order['bufee'];
	if($bufee>0){
		model('core/member')->member_credit($order['mid'],'credit2',$bufee,true,'jy_kuaidisys','多退快递费用');
		$this->pdo_update('jilog',array('isbupay'=>1,'status'=>2),array('id'=>$order['id']));
	}else{
		exi('多退异常！可退金额不足！');
	}
	exi('多退成功！已退到余额账户！');
}
//支付订单
if($_RQ['op']=='bupay'){
	$id=intval($_RQ['id']);
	$order=pdo_get('jy_kuaidisys_jilog',array('id'=>$id));
	if($order['bupay_id']){
		$payid=$order['bupay_id'];
		$order_id=$order['id'];
	}else{
		$order_id=$order['id'];
		$params=array(
			'id'=>$order_id,
			'title'=>'快递补款',
			'fee'=>$order['bufee'],
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_kuaidisys/ji_log/bupayresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>'jy_kuaidisys/ji_log/',
			'error_url'=>'jy_kuaidisys/ji_log/',
		);		
		$payid=cfc('pay')->create_pay_order($params);
		$this->pdo_update('jilog',array('bupay_id'=>$payid),array('id'=>$order_id));
	}
	exi($payid);
}
if($_RQ['op']=='bupayresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_kuaidisys/ji_log/');
	}
	$order=pdo_get('jy_kuaidisys_jilog',array('id'=>$payresult['id']));
	if($order['isbupay'] ==1){
		exi('该订单已经支付！','error','jy_kuaidisys/ji_log/');
	}
	if($order['bufee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['bufee'].'|'.$payresult['fee'],'error','jy_kuaidisys/ji_log/');
	}
	$this->pdo_update('jilog',array('isbupay'=>1,'status'=>2),array('id'=>$order['id']));
	exi('提交成功!');
}
?>